import { _decorator, Component, Node, director,Label, UITransform, UIOpacity, sp,AudioSource, assert  } from 'cc';
const { ccclass, property} = _decorator;
import Common from './Common';   

@ccclass('MathGame')
export class MathGame extends Component {

    @property(Node)
    private settingNode: Node = null;


    //最大数
    @property(Label)
    private maxNumberLabel: Label = null;
    private maxNumber: number = 10;

    @property(Node)
    private button_top1: Node = null;

    @property(Node)
    private button_top2: Node = null;
  
    
    //运算符号
    @property(Node)
    private button_two1: Node = null;
    @property(Node)
    private button_two2: Node = null;
    @property(Node)
    private button_two3: Node = null;
    @property(Node)
    private button_two4: Node = null;

    private opt1:number=1;//加
    private opt2:number=1;//减
    private opt3:number=1;//乘
    private opt4:number=1;//除

    
    //题数

    @property(Label)
    private countNumberLabel: Label = null;
    private countNumber: number = 10;

    @property(Node)
    private button_three1: Node = null;

    @property(Node)
    private button_three2: Node = null;


    //速度
    @property(Node)
    private button_four1: Node = null;

    @property(Node)
    private button_four2: Node = null;

    @property(Node)
    private button_four3: Node = null;

    private speed:number=1;


    @property(AudioSource)
    public _audioSource: AudioSource = null!;


    onLoad () {
        
    }



    //初始化隐藏设置框
    start() {
        this.settingNode.active=false;
        //设置透明度
        //const opacityComp = this.settingNode.getComponent(UIOpacity);
        //opacityComp.opacity = 1;
        console.log('start')
        this.initConfig();

       // 获取 AudioSource 组件
        const audioSource = this.node.getComponent(AudioSource)!;
       // 检查是否含有 AudioSource，如果没有，则输出错误消息
        assert(audioSource);
         // 将组件赋到全局变量 _audioSource 中
         this._audioSource = audioSource;


 // 播放音乐
 this._audioSource.play();



    }

    /**
     * initConfig
     */
    public initConfig() {
        this.opt1=Common.opt1;
        this.opt2=Common.opt2;
        this.opt3=Common.opt3;
        this.opt4=Common.opt4;


        this.countNumber=Common.countNumber;
        this.maxNumber=Common.maxNumber;
     
        this.speed=Common.speed;


        this.setMaxNumber(0);
        this.setOpt(1);
        this.setOpt(2);
        this.setOpt(3);
        this.setOpt(4);
        this.setCountNumber(0);
        this.setSpeed(this.speed);
    }


    //最大数
    private setMaxNumber(type:number){

        if(type==-1){
            this.maxNumber=this.maxNumber-10;
        }
        if(type==1){
            this.maxNumber=this.maxNumber+10;
        }
        
        if(this.maxNumber>=100){
            this.maxNumber=100;
        }
        if(this.maxNumber<10){
            this.maxNumber=10;
        }

        this.maxNumberLabel.string=this.maxNumber+'';    
        const opacitybutton_top1 = this.button_top1.getComponent(UIOpacity);
        const opacitybutton_top2 = this.button_top2.getComponent(UIOpacity);
       
        if(this.maxNumber==100){
            opacitybutton_top1.opacity=128;
            opacitybutton_top2.opacity = 255;
        }
        if(this.maxNumber==10){
            opacitybutton_top1.opacity=255;
            opacitybutton_top2.opacity = 128;
        }

    }

    private onBtnSetMaxNumberAdd(){
       this.setMaxNumber(1);
    }
    private onBtnSetMaxNumberSub(){
        this.setMaxNumber(-1);
    }

    //加减乘除,保留1个
    private setOpt(type:number){

       

        if(type==1){
            
            const opacitybutton_two1 = this.button_two1.getComponent(UIOpacity);
            if(this.opt1==1){
                opacitybutton_two1.opacity=255;
               
            }else{
                opacitybutton_two1.opacity=128;
            }
        }
        if(type==2){
            
            const opacitybutton_two2 = this.button_two2.getComponent(UIOpacity);

            if(this.opt2==1){
                opacitybutton_two2.opacity=255;
            }else{
                opacitybutton_two2.opacity=128;
            }

        }
        if(type==3){
            
            const opacitybutton_two3 = this.button_two3.getComponent(UIOpacity);
            if(this.opt3==1){
                opacitybutton_two3.opacity=255;
            }else{
                opacitybutton_two3.opacity=128;
            }
        }
        if(type==4){
            
            const opacitybutton_two4 = this.button_two4.getComponent(UIOpacity);
            if(this.opt4==1){
                opacitybutton_two4.opacity=255;
            }else{
                opacitybutton_two4.opacity=128;
            }
        }

    }

    private setOpt1(){
        if(this.opt2==-1&&this.opt3==-1&&this.opt4==-1){
            this.opt1=1;
        }else{
            this.opt1=this.opt1*-1;
        }

       
        this.setOpt(1);

        console.log(this.opt1)
    }
    private setOpt2(){
        if(this.opt1==-1&&this.opt3==-1&&this.opt4==-1){
            this.opt2=1;
        }else{
            this.opt2=this.opt2*-1;
        }

        this.setOpt(2);
        console.log(this.opt2)
    }
    private setOpt3(){
        if(this.opt1==-1&&this.opt2==-1&&this.opt4==-1){
            this.opt3=1;
        }else{
            this.opt3=this.opt3*-1;
        }
        this.setOpt(3);
        console.log(this.opt3)
    }
    private setOpt4(){
        if(this.opt1==-1&&this.opt2==-1&&this.opt3==-1){
            this.opt4=1;
        }else{
            this.opt4=this.opt4*-1;
        }
        this.setOpt(4);
        console.log(this.opt4)
    }

    //题数
    private setCountNumber(type:number){

        if(type==-1){
            this.countNumber=this.countNumber-10;
        }
        if(type==1){
            this.countNumber=this.countNumber+10;
        }
        
        if(this.countNumber>=50){
            this.countNumber=50;
        }
        if(this.countNumber<10){
            this.countNumber=10;
        }

        this.countNumberLabel.string=this.countNumber+'';    
        const opacitybutton_three1 = this.button_three1.getComponent(UIOpacity);
        const opacitybutton_three2= this.button_three2.getComponent(UIOpacity);
       
        if(this.countNumber==50){
            opacitybutton_three1.opacity=128;
            opacitybutton_three2.opacity = 255;
        }
        if(this.countNumber==10){
            opacitybutton_three1.opacity=255;
            opacitybutton_three2.opacity = 128;
        }

    }

    private onBtnSetCountNumberAdd(){
       this.setCountNumber(1);
    }
    private onBtnSetCountNumberSub(){
        this.setCountNumber(-1);
    }

    //速度
    private setSpeed(speed:number){
        this.speed=speed;
        const opacitybutton_four1 = this.button_four1.getComponent(UIOpacity);
        const opacitybutton_four2 = this.button_four2.getComponent(UIOpacity);
        const opacitybutton_four3 = this.button_four3.getComponent(UIOpacity);
        opacitybutton_four1.opacity=128;
        opacitybutton_four2.opacity=128;
        opacitybutton_four3.opacity=128;
        if(speed==1){   
            opacitybutton_four1.opacity=255;
        }
        if(speed==2){
            opacitybutton_four2.opacity=255;
        }
        if(speed==3){
            opacitybutton_four3.opacity=255;     
        }

    }
    private onBtnSetSpeed1(){
        this.setSpeed(1);
    }
    private onBtnSetSpeed2(){
        this.setSpeed(2);
    }
    private onBtnSetSpeed3(){
        this.setSpeed(3);
    }



    private onBtnStart() {
        
       console.log('bbbbbbbbbbbbbbb');
       console.log(Common.speed);
       director.loadScene("mathgame");
    }
    private onBtnSetting() {
        this.settingNode.active=true;
    }
    private closeSetting(){
        Common.countNumber=this.countNumber;
        Common.maxNumber=this.maxNumber;
        Common.opt1=this.opt1;
        Common.opt2=this.opt2;
        Common.opt4=this.opt3;
        Common.opt4=this.opt4;
        Common.speed=this.speed;

       

        this.settingNode.active=false;
    }

}

